package com.cn.intelligence.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbBrandMapper;
import com.cn.intelligence.pojo.EUDataGridResult;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.pojo.TbBrand;
import com.cn.intelligence.pojo.TbBrandExample;
import com.cn.intelligence.service.BrandService;
import com.cn.intelligence.util.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	TbBrandMapper tbBrandMapper;
	@Override
	public EUDataGridResult getBrandList(Integer page, Integer rows,String filename) {
		TbBrandExample tbBrandExample = new TbBrandExample();
		TbBrandExample.Criteria critera = tbBrandExample.createCriteria();
		if(filename !=null && !"".equals(filename)){
			critera.andNameLike("%"+filename+"%");
		}
		// 分页查询
		PageHelper.startPage(page, rows);
		List<TbBrand> tbBrandList = tbBrandMapper.selectByExample(tbBrandExample);
		// 获取总数
		PageInfo<TbBrand> pageInfo = new PageInfo<TbBrand>(tbBrandList);
		// 实现返回值设定
		EUDataGridResult euDataGridResult = new EUDataGridResult();
		euDataGridResult.setRows(tbBrandList);
		euDataGridResult.setTotal(pageInfo.getTotal());
		return euDataGridResult;

	}
	@Override
	public TaotaoResult addBrand(TbBrand tbBrand) {
		tbBrand.setId(IDUtils.genItemId());
		tbBrand.setCreated(new Date());
		tbBrand.setUpdated(new Date());
		int value = tbBrandMapper.insert(tbBrand);
		if(value==1){
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.fail();
		}
	}
	@Override
	public TaotaoResult deleteBrand(Long[] ids) {
		TbBrandExample tbBrandExample = new TbBrandExample();
		TbBrandExample.Criteria criteria = tbBrandExample.createCriteria();
		criteria.andIdIn(Arrays.asList(ids));
		int value = tbBrandMapper.deleteByExample(tbBrandExample);	
		if(value == ids.length){
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.fail();
		}
	}

}
